package com.yq.testmodule.vp2;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ScrollView;

public class MyScrollView extends ScrollView {
    public MyScrollView(Context context) {
        super(context);
    }

    public MyScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }


    float mLastY,mLastX;




    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        getParent().requestDisallowInterceptTouchEvent(true);
        if(action == MotionEvent.ACTION_DOWN){
            mLastY = ev.getY();
            mLastX = ev.getX();

        }else if (action == MotionEvent.ACTION_MOVE) {
            float y = ev.getY();
            float x = ev.getX();
            float dy = y - mLastY;
            float dx = x - mLastX;
            if (Math.abs(dy) > Math.abs(dx)) {
                if (dy < 0) {
                    //到底了
                    if(isScrollToBottom()){
                        Log.e("YYYY","到底了");
                        getParent().requestDisallowInterceptTouchEvent(false);
                    }
                } else {
                    //到头了
                    if(isScrollToTop()){
                        Log.e("YYYY","到头了");
                        getParent().requestDisallowInterceptTouchEvent(false);
                    }
                }
            }else {
                getParent().requestDisallowInterceptTouchEvent(false);
            }
            mLastX = x;
            mLastY = y;
        }

        return super.dispatchTouchEvent(ev);
    }



    //判断Scrollview是否滑动到底部
    public boolean isScrollToBottom() {
        return getScrollY() + getHeight() >= computeVerticalScrollRange();
    }
    //判断Scrollview是否滑动到顶部
    public boolean isScrollToTop() {
        return getScrollY() == 0;
    }

}
